import java.util.Arrays;

/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-09-22
 * Time:21:25
 */

//泛型 ：<T> :占位符 ，表示当前的类是一个泛型类
class  MyArrayList <T>{
    public T[] elem;
    public int  usedSize;

    public MyArrayList(){

        this.elem = (T[]) new Object[10];//知道有这个问题
    }

    public  void add(T val){
        this.elem[this.usedSize] = val;
        this.usedSize++;
    }

    public T get(int pos){

        return this.elem[pos];
    }
}
//class  MyArrayList{
//    public int[] elem;
//    public int usedSize;
//
//    public MyArrayList(){
//        this.elem = new int[10];
//    }
//
//    public  void add(int val){
//        this.elem[this.usedSize] = val;
//        this.usedSize++;
//    }
//
//    public int get(int pos){
//        return this.elem[pos];
//    }
//}

class   Person{

}
public class TestDemo2 {
    public static Object[] getArray(int size){
        Object[] genericArray = new Object[size];
        return genericArray;
    }
    public static void main(String[] args) {
        String[] myArray = (String[]) getArray(5);//不能进行类型转化
    }
    public static void main3(String[] args) {
        Person person = new Person();
        System.out.println(person);
        MyArrayList<String> myArrayList = new MyArrayList<>();
        System.out.println(myArrayList);
        MyArrayList<Integer> myArrayList1 = new MyArrayList<>();
        System.out.println(myArrayList1);
        MyArrayList<Double> myArrayList2 = new MyArrayList<>();
        System.out.println(myArrayList2);
        MyArrayList<Person> myArrayList3 = new MyArrayList<>();
        System.out.println(myArrayList3);
    }
    public static void main2(String[] args) {
        MyArrayList<String> myArrayList = new MyArrayList<>();
        myArrayList.add("hello");
//        myArrayList.add(1);//自动进行类型检查
        myArrayList.add("hello2");
        //2.自动进行类型的转换
        String s = myArrayList.get(0);
        System.out.println(s);
        //3.泛型的参数一定是类类型 如果是简单类型要放他的包装类

        //4.泛型是怎样编译的？
        //机制：擦除机制，只作用于编译期间，换句话说就是编译时的一种机制 都会擦除为Obeject
        MyArrayList<Integer> myArrayList1 = new MyArrayList<>();

        MyArrayList<Double> myArrayList2 = new MyArrayList<>();

        MyArrayList<Person> myArrayList3 = new MyArrayList<>();
//        System.out.println(Arrays.toString(myArrayList));
    }
    public static void main1(String[] args) {
        MyArrayList myArrayList = new MyArrayList();
        myArrayList.add(1);
//        myArrayList.add("fafafa");
        int val = (int) myArrayList.get(0);
//        int val = myArrayList.get(0);
//        System.out.println(val);
    }
}
